package com.autoplaza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ResultadoBusqueda extends ListActivity {
	String result, marcaseleccionada, modeloseleccionado,varseleccionada,sb9;
	InputStream is = null;
	String[] arr2,array2, array4;
	private LayoutInflater mInflater;
	private Vector<RowData> data;
	String textoEnviar2;
	String cadena2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.resultadobusqueda);
		
	    Bundle extras=getIntent().getExtras();
			if(extras!=null){
				sb9=extras.getString("sb9");
			}
			organizararray(sb9);
		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        data = new Vector<RowData>();
        Log.i("TAMAÑO 4", ""+array4.length);
        Log.i("TAMAÑO 2", ""+array2.length);
		for (int i = 0; i < array2.length-1; i++) {
			RowData rd = new RowData(""+array2[i]," ("+array4[i]+") ");
	        data.add(rd);
		}
		CustomAdapter adapter = new CustomAdapter(getApplicationContext(), R.layout.custom_row2,R.id.item, data);
        setListAdapter(adapter); 
	}
	private class RowData {
	    protected String mItem;
	        protected String mDescription;
	        RowData(String item, String description){
	        mItem = item;
	        mDescription = description;             
	    }
	        @Override
	        public String toString() {
	                return mDescription + " " +   mItem;
	        }
	}
	
	private class CustomAdapter extends ArrayAdapter<RowData> {
        public CustomAdapter(Context context, int resource,
                        int textViewResourceId, List<RowData> objects) {
                super(context, resource, textViewResourceId, objects);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;

                //widgets displayed by each item in your list
                TextView item = null;
                TextView description = null;

                //data from your adapter
                RowData rowData= getItem(position);

                //we want to reuse already constructed row views...
                if(null == convertView){
                        convertView = mInflater.inflate(R.layout.custom_row2, null);
                        holder = new ViewHolder(convertView);
                        convertView.setTag(holder);
                }
                holder = (ViewHolder) convertView.getTag();
                item = holder.getItem();
                item.setText(rowData.mItem);
                item.setTextSize(19);
    			item.setTextColor(Color.BLACK);

                description = holder.getDescription();          
                description.setText(rowData.mDescription);
                description.setTextSize(16);
    			description.setTextColor(Color.BLACK);
                return convertView;
        }
}
	private class ViewHolder {      
	    private View mRow;
	    private TextView description = null;
	    private TextView item = null;
	        public ViewHolder(View row) {
	        mRow = row;
	        }
	        public TextView getDescription() {
	                if(null == description){
	                        description = (TextView) mRow.findViewById(R.id.item);
	                }
	                return description;
	        }

	        public TextView getItem() {
	                if(null == item){
	                        item = (TextView) mRow.findViewById(R.id.description);
	                }
	                return item;
	        }       
	}
	
	public void onListItemClick(ListView parent, View v, int position, long id) {
	    CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
        SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = preferences.edit();
			editor.putString("ano", array2[position]);
			editor.commit();
			finish();
	}
	
	protected void organizararray(String sb32) {
		 String url=sb32;
	        Log.i("url mia", ""+url);
			URI myURI = null;
			try {
				myURI = new URI(url);
			} catch (URISyntaxException e) {
			}
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet getMethod = new HttpGet(myURI);
			HttpResponse webServerResponse = null;
			try {
				webServerResponse = httpClient.execute(getMethod);
			}  catch (IOException e) {
			}
			
			HttpEntity httpEntity = webServerResponse.getEntity();
			//MANEJO DE LA RESPUESTA DEL SERVIDOR
			if (httpEntity != null) {
				try {
					is = httpEntity.getContent();
					BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
		            StringBuilder sb = new StringBuilder();
		            String line = null;
		            while ((line = reader.readLine()) != null) {
		                    sb.append(line + "\n");
		            }
		            is.close();
		            result=sb.toString();
		            String array[]=result.split("%%%");
		            Log.i("result", ""+result);
		            array2 = array[1].split("%%%");
		            for(int i=0; i<array.length; i++){
		            	Log.i("MI ARRAY1", ""+array[i]);
		            }
		            array2 = new String[array.length];
		            for(int i=0; i<array.length; i++){
		            	String[] array3=array[i].toString().split("@@@");
		            	array2[i]=array3[0];
		            	Log.i("MI ARRAY2", ""+array2[i]);
		            }
		            array4 = new String[array.length];
		            for(int i=0; i<array.length-1; i++){
		            	String[] array3=array[i].toString().split("@@@");
		            	array4[i]=array3[1];
		            	Log.i("MI ARRAY4", ""+array4[i]);
		            }
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
				}
			}
	}

	public void onBackPressed() {
	}	
}
